﻿using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web.Http;
using wepoverkefni5.Models;
using System.Net;
using System.Web.Mvc;
using System.IO;
using System;
using System.Web;
using System.Diagnostics;

namespace wepoverkefni5.Controllers
{
    /// <summary>
    /// API Controller for handling all playlist CRUD operations except for the functionality
    /// of creating a new playlist, which is implemented in a separate controller due to 
    /// complications in how to invoke to different post request´s with similar parameters
    /// </summary>
    public class PlaylistController : ApiController
    {
        DataRepository m_rep = new DataRepository();

        /// <summary>
        /// GET all the playlists
        /// </summary>
        /// <returns>IQueryable playlist collection</returns>
        public IQueryable<Playlist> GetPlaylists()
        {
            var result = m_rep.GetAllPlaylists();
            if (result == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            return result;
        }

        /// <summary>
        /// GET all songs belonging to playlist
        /// </summary>
        /// <param name="id">The id of the playlist beeing requested</param>
        /// <returns>IQueryable songs collection</returns>
        public IQueryable<Song> GetPlaylistSongs(int id)
        {
            //System.Diagnostics.Debug.WriteLine("Getting songs for playlist with id:" + id);
            var result = m_rep.GetPlaylistSongs(id);
            if (result == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            return result;
        }

        /// <summary>
        /// Add a song to playlist. 
        /// </summary>
        /// <param name="model">PlaylistAddModel is a model containing 2 int variables which represent both song and playlist id</param>
        /// <returns>HttpResponseMessage response</returns>
        public HttpResponseMessage PostAddSong(PlaylistAddModel model)
        {
            //System.Diagnostics.Debug.WriteLine("Trying to add song to playlist!");
            if (model == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            PlaylistSong song = new PlaylistSong();
            song.playlistId = model.playlistId;
            song.songId = model.songId;
            m_rep.addNewPlaylistSong(song);

            HttpResponseMessage response = new HttpResponseMessage();
            response.StatusCode = HttpStatusCode.Created;
            return response;
        }

        /// <summary>
        /// Delete a playlist
        /// </summary>
        /// <param name="id">Represents the id of the playlist to delete</param>
        /// <returns>HttpResponseMessage response</returns>
        public HttpResponseMessage DeletePlaylist(int id)
        {
            var list = m_rep.getSinglePlaylist(id);
            var listSongs = m_rep.getPlaylistSongsByPlaylistId(id);
            if (list == null || listSongs == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            m_rep.deletePlaylistSongs(listSongs);
            m_rep.deletePlaylist(list);
            HttpResponseMessage response = new HttpResponseMessage();
            response.StatusCode = HttpStatusCode.OK;
            return response;
        }

        /// <summary>
        /// Delete song from a playlist
        /// </summary>
        /// <param name="id">Represents the id of the song to delete</param>
        /// <returns></returns>
        //public HttpResponseMessage DeletePlaylistSong(int id)
        //{
        //    var result = m_rep.getPlaylistItem(id);
        //    if (result == null)
        //    {
        //        throw new HttpResponseException(HttpStatusCode.NotFound);
        //    }
        //    m_rep.deletePlaylistSong(result);
        //    HttpResponseMessage response = new HttpResponseMessage();
        //    response.StatusCode = HttpStatusCode.OK;
        //    return response;
        //}
    }
}
